package com.zhao.redis;

import jdk.nashorn.internal.ir.CallNode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.ListPosition;

import java.util.List;
import java.util.Set;

//jedis操作set相关的指令
public class TestSet {

    private Jedis jedis;

    @Before
    public void before() {
        jedis = new Jedis("192.168.126.100", 6379);
    }

    @After
    public void after() {
        jedis.close();
    }

    @Test
    public void testSadd() {
        jedis.sadd("foods", "fish", "apple", "water");
        jedis.sadd("fruit", "banana", "berry", "water");
    }

    @Test
    public void testSmembers() {
        Set<String> foods = jedis.smembers("foods");
        foods.forEach(food -> System.out.println(food));
        System.out.println("======================");
        Set<String> fruits = jedis.smembers("fruit");
        fruits.forEach(fruit -> System.out.println(fruit));
    }

    @Test
    public void testScard() {
        long num = jedis.scard("foods");
        System.out.println("num: " + num);
    }

    @Test
    public void testSpop() {
        String food = jedis.spop("foods");
        System.out.println(food);
    }

    @Test
    public void testSmove() {
        jedis.smove("foods", "fruit", "apple");
    }

    @Test
    public void testSismember() {
        boolean sismember = jedis.sismember("foods", "apple");
        System.out.println(sismember);
    }

    @Test
    public void testSrem() {
        jedis.srem("foods", "fish");
    }

    @Test
    public void testSrandmember() {
        String fruit = jedis.srandmember("fruit");
        System.out.println(fruit);
    }

    @Test
    public void testSdiff() {
        Set<String> fruits = jedis.sdiff("fruit", "foods");
        fruits.forEach(fruit -> System.out.println(fruit));
    }

    @Test
    public void testSinter() {
        Set<String> fruits = jedis.sinter("fruit", "foods");
        fruits.forEach(fruit -> System.out.println(fruit));
    }

    @Test
    public void testSunion() {
        Set<String> fruits = jedis.sunion("fruit", "foods");
        fruits.forEach(fruit -> System.out.println(fruit));
    }

}
